package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

import org.hibernate.criterion.Order;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

import com.aplikata.constants.ConstantsMsg;
import com.aplikata.evok.bo.Result;
import com.aplikata.evok.bo.ResultDetail;
import com.aplikata.evok.datamodel.ResultDataModel;
import com.aplikata.evok.datamodel.ResultDetailDataModel;
import com.aplikata.evok.service.EvokQtnaireService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;
import com.aplikata.util.StringUtil;

@ManagedBean(name = "resultBean")
@RequestScoped
public class EvokResultBean extends UIBean {
	private static final long serialVersionUID = 1L;
	@ManagedProperty(value = "#{evokQtnaireService}")
	private EvokQtnaireService evokQtnaireService;
	private Result result;
	private List<Result> resultList;
	private ResultDataModel resultModel;
	private ResultDetail resultDetail;
	private List<ResultDetail> resultDetailList;
	private ResultDetailDataModel resultDetailModel;

	public EvokResultBean() {
		result = new Result();
	}

	public void resultEdit(RowEditEvent e) {
		int index = resultList.indexOf(e.getObject());
		result = (Result) e.getObject();
		if (StringUtil.isEmpty(result.getName())) {
			FacesUtil.addErrorMessage(ConstantsMsg.MSG_REQUIRE, new Object[]{FacesUtil.getMessage("default_name")});
			return;
		}
		Result temp = (Result) evokQtnaireService.merge(result);
		resultList.remove(index);
		resultList.add(index, temp);
		FacesUtil.addInfoMessage("info_content_edit_success", new Object[] { result.getName() });
	}

	public void resultDetailEdit(RowEditEvent e) {
		int index = resultDetailList.indexOf(e.getObject());
		resultDetail = (ResultDetail) e.getObject();
		if (FacesUtil.isKeyNull(resultDetail.getName())) {
			FacesUtil.addErrorMessage(ConstantsMsg.MSG_REQUIRE, new Object[]{FacesUtil.getMessage("default_name")});
			return;
		}

		ResultDetail temp = (ResultDetail) evokQtnaireService.merge(resultDetail);
		resultDetailList.remove(index);
		resultDetailList.add(index, temp);
		FacesUtil.addInfoMessage("info_content_edit_success", new Object[] { resultDetail.getName()});
	}

	@SuppressWarnings("unchecked")
	public void resultSelect(SelectEvent event) {
		result = (Result) event.getObject();
		if (result == null || FacesUtil.isKeyNull(result.getId()))
			return;
		resultDetailList = (List<ResultDetail>) evokQtnaireService.findByNamedQuery(ResultDetail.QY_FIND_BY_RESULT,
				new Object[] { result.getId() });
	}

	public void resultDetailSelect(SelectEvent event) {
		resultDetail = (ResultDetail) event.getObject();
	}

	public void deleteResultAction(ActionEvent event) {
		try {
			if (result == null || FacesUtil.isKeyNull(result.getId())) {
				if (resultList.contains(result))
					resultList.remove(result);
				return;
			}
			evokQtnaireService.deleteByKey(Result.class, result.getId());
			FacesUtil.addInfoMessage("info_content_delete_success", new Object[] { result.getName() });
			resultList.remove(result);
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
		}
	}

	public void deleteResultDetailAction(ActionEvent event) {
		try {
			if (resultDetail == null || FacesUtil.isKeyNull(resultDetail.getId())) {
				if (resultDetailList.contains(resultDetail))
					resultDetailList.remove(resultDetail);
				return;
			}
			evokQtnaireService.deleteByKey(ResultDetail.class, resultDetail.getId());
			FacesUtil.addInfoMessage("info_content_delete_success", new Object[] { resultDetail.getName() });
			resultDetailList.remove(resultDetail);
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
			return;
		}
	}

	public void addResultAction(ActionEvent event) {
		result = new Result();
		resultList.add(0, result);
	}

	public void addResultDetailAction(ActionEvent event) {
		if (result == null || FacesUtil.isKeyNull(result.getId()))
			return;

		resultDetail = new ResultDetail(result);
		resultDetailList.add(0, resultDetail);
	}

	public EvokQtnaireService getEvokQtnaireService() {
		return evokQtnaireService;
	}

	public void setEvokQtnaireService(EvokQtnaireService evokQtnaireService) {
		this.evokQtnaireService = evokQtnaireService;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	@SuppressWarnings("unchecked")
	public List<Result> getResultList() {
		if (resultList == null)
			resultList = (List<Result>) evokQtnaireService.findAll(Result.class, new Order[] { Order.asc("name") });
		return resultList;
	}

	public void setResultList(List<Result> resultList) {
		this.resultList = resultList;
	}

	public ResultDetail getResultDetail() {
		return resultDetail;
	}

	public void setResultDetail(ResultDetail resultDetail) {
		this.resultDetail = resultDetail;
	}
	
	

	@SuppressWarnings("unchecked")
	public List<ResultDetail> getResultDetailList() {
		if (result == null || FacesUtil.isKeyNull(result.getId()))
			return null;

		if (resultDetailList == null) {
			resultDetailList = (List<ResultDetail>) evokQtnaireService.findByNamedQuery(ResultDetail.QY_FIND_BY_RESULT,
					new Object[] { result.getId() });
		}
		return resultDetailList;
	}

	public void setResultDetailList(List<ResultDetail> resultDetailList) {
		this.resultDetailList = resultDetailList;
	}

	public ResultDataModel getResultModel() {
		if (resultModel == null) {
			resultModel = new ResultDataModel(evokQtnaireService, getResultList());
		}
		return resultModel;
	}

	public void setResultModel(ResultDataModel resultModel) {
		this.resultModel = resultModel;
	}

	public ResultDetailDataModel getResultDetailModel() {
		if (resultDetailModel == null) {
			resultDetailModel = new ResultDetailDataModel(evokQtnaireService, getResultDetailList());
		}
		return resultDetailModel;
	}

	public void setResultDetailModel(ResultDetailDataModel resultDetailModel) {
		this.resultDetailModel = resultDetailModel;
	}

}
